/**
 * 颜色转换(16进制转rgba)
 * @param colorStr - 16进制颜色
 * @param opacity - 透明度
 */
export function hexToRgba(colorStr: string, opacity = 1): string | false {
  let colorStrNew = (colorStr || '').toLowerCase().replace(/\s*/g, '');
  if (colorStrNew && /^#([0-9|a-f]{3}|[0-9|a-f]{6})$/.test(colorStrNew)) {
    if (colorStrNew.length === 4) {
      // 将三位转换为六位
      colorStrNew = `'#'${colorStrNew[1]}${colorStrNew[1]}`;
      colorStrNew += `${colorStrNew[2]}${colorStrNew[2]}`;
      colorStrNew += `${colorStrNew[2]}${colorStrNew[2]}`;
    }
    // 处理六位的颜色值
    const color = [];
    for (let i = 1; i < 7; i += 2) {
      const item = `0x${colorStrNew.slice(i, i + 2)}`;
      color.push(parseInt(item));
    }
    return `rgba(${color[0]},${color[1]},${color[2]},${opacity})`;
  } else {
    console.error('颜色不合法===', colorStrNew);
    return false;
  }
}
